
package org.tinygroup.sdpm.zhang.dao.impl;

import static org.tinygroup.sdpm.zhang.dao.inter.constant.GoodsorderTable.GOODSORDER_TABLE;
import static org.tinygroup.tinysqldsl.Delete.delete;
import static org.tinygroup.tinysqldsl.Insert.insertInto;
import static org.tinygroup.tinysqldsl.Select.selectFrom;
import static org.tinygroup.tinysqldsl.Update.update;
import static org.tinygroup.tinysqldsl.base.StatementSqlBuilder.and;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.tinygroup.commons.tools.CollectionUtil;
import org.tinygroup.jdbctemplatedslsession.callback.DeleteGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.InsertGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.NoParamDeleteGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.NoParamInsertGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.NoParamUpdateGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.SelectGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.UpdateGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.daosupport.OrderBy;
import org.tinygroup.jdbctemplatedslsession.daosupport.TinyDslDaoSupport;
import org.tinygroup.jdbctemplatedslsession.util.TinyDSLUtil;
import org.tinygroup.sdpm.zhang.dao.inter.GoodsorderDao;
import org.tinygroup.sdpm.zhang.dao.inter.pojo.Goodsorder;
import org.tinygroup.tinysqldsl.Delete;
import org.tinygroup.tinysqldsl.Insert;
import org.tinygroup.tinysqldsl.Pager;
import org.tinygroup.tinysqldsl.Select;
import org.tinygroup.tinysqldsl.Update;
import org.tinygroup.tinysqldsl.expression.JdbcNamedParameter;

/**
 * <!-- begin-user-doc --> 如果不希望某方法或者变量被覆盖，可以删除方法或者变量的注释@modifiable <!--
 * end-user-doc -->
 */
@Repository
public class GoodsorderDaoImpl extends TinyDslDaoSupport implements GoodsorderDao {

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public Goodsorder add(Goodsorder goodsorder) {
		return getDslTemplate().insertAndReturnKey(goodsorder, new InsertGenerateCallback<Goodsorder>() {
			public Insert generate(Goodsorder t) {
				Insert insert = insertInto(GOODSORDER_TABLE).values(GOODSORDER_TABLE.ORDER_ID.value(t.getOrderId()),
						GOODSORDER_TABLE.USER_ID.value(t.getUserId()), GOODSORDER_TABLE.GOODS_ID.value(t.getGoodsId()),
						GOODSORDER_TABLE.OUT_TIME.value(t.getOutTime())

				);
				return insert;
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int edit(Goodsorder goodsorder) {
		if (goodsorder == null || StringUtils.isBlank(goodsorder.getOrderId())) {
			return 0;
		}
		return getDslTemplate().update(goodsorder, new UpdateGenerateCallback<Goodsorder>() {
			public Update generate(Goodsorder t) {
				Update update = update(GOODSORDER_TABLE)
						.set(GOODSORDER_TABLE.USER_ID.value(t.getUserId()),
								GOODSORDER_TABLE.GOODS_ID.value(t.getGoodsId()),
								GOODSORDER_TABLE.OUT_TIME.value(t.getOutTime()))
						.where(GOODSORDER_TABLE.ORDER_ID.eq(t.getOrderId()));
				return update;
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int deleteByKey(String pk) {

		if (StringUtils.isBlank(pk)) {
			return 0;
		}
		return getDslTemplate().deleteByKey(pk, new DeleteGenerateCallback<Serializable>() {
			public Delete generate(Serializable pk) {
				return delete(GOODSORDER_TABLE).where(GOODSORDER_TABLE.ORDER_ID.eq(pk));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int deleteByKeys(String... pks) {
		if (pks == null || pks.length == 0) {
			return 0;
		}
		for (String pk : pks) {
			if (StringUtils.isBlank(pk)) {
				return 0;
			}
		}
		return getDslTemplate().deleteByKeys(new DeleteGenerateCallback<Serializable[]>() {
			public Delete generate(Serializable[] t) {
				return delete(GOODSORDER_TABLE).where(GOODSORDER_TABLE.ORDER_ID.in(t));
			}
		}, pks);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public Goodsorder getByKey(String pk) {
		return getDslTemplate().getByKey(pk, Goodsorder.class, new SelectGenerateCallback<Serializable>() {

			@SuppressWarnings("rawtypes")
			public Select generate(Serializable t) {
				return selectFrom(GOODSORDER_TABLE).where(GOODSORDER_TABLE.ORDER_ID.eq(t));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public List<Goodsorder> query(Goodsorder goodsorder, final OrderBy... orderArgs) {
		if (goodsorder == null) {
			goodsorder = new Goodsorder();
		}
		return getDslTemplate().query(goodsorder, new SelectGenerateCallback<Goodsorder>() {
			@SuppressWarnings("rawtypes")
			public Select generate(Goodsorder t) {
				Select select = selectFrom(GOODSORDER_TABLE).where(and(GOODSORDER_TABLE.ORDER_ID.eq(t.getOrderId()),
						GOODSORDER_TABLE.USER_ID.eq(t.getUserId()), GOODSORDER_TABLE.GOODS_ID.eq(t.getGoodsId()),
						GOODSORDER_TABLE.OUT_TIME.eq(t.getOutTime())

				));
				return TinyDSLUtil.addOrderByElements(select, orderArgs);
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public Pager<Goodsorder> queryPager(int start, int limit, Goodsorder goodsorder, final OrderBy... orderArgs) {
		if (goodsorder == null) {
			goodsorder = new Goodsorder();
		}
		return getDslTemplate().queryPager(start, limit, goodsorder, false, new SelectGenerateCallback<Goodsorder>() {
			public Select generate(Goodsorder t) {
				Select select = Select.selectFrom(GOODSORDER_TABLE).where(and(
						GOODSORDER_TABLE.ORDER_ID.eq(t.getOrderId()), GOODSORDER_TABLE.USER_ID.eq(t.getUserId()),
						GOODSORDER_TABLE.GOODS_ID.eq(t.getGoodsId()), GOODSORDER_TABLE.OUT_TIME.eq(t.getOutTime())

				));
				return TinyDSLUtil.addOrderByElements(select, orderArgs);
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] batchInsert(boolean autoGeneratedKeys, List<Goodsorder> goodsorder) {
		if (CollectionUtil.isEmpty(goodsorder)) {
			return new int[0];
		}
		return getDslTemplate().batchInsert(autoGeneratedKeys, goodsorder, new InsertGenerateCallback<Goodsorder>() {

			public Insert generate(Goodsorder t) {
				return insertInto(GOODSORDER_TABLE).values(GOODSORDER_TABLE.ORDER_ID.value(t.getOrderId()),
						GOODSORDER_TABLE.USER_ID.value(t.getUserId()), GOODSORDER_TABLE.GOODS_ID.value(t.getGoodsId()),
						GOODSORDER_TABLE.OUT_TIME.value(t.getOutTime())

				);
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] batchInsert(List<Goodsorder> goodsorders) {
		return batchInsert(false, goodsorders);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] batchUpdate(List<Goodsorder> goodsorders) {
		if (CollectionUtil.isEmpty(goodsorders)) {
			return new int[0];
		}
		for (Goodsorder goodsorder : goodsorders) {
			if (StringUtils.isBlank(goodsorder.getOrderId())) {
				return new int[0];
			}
		}
		return getDslTemplate().batchUpdate(goodsorders, new UpdateGenerateCallback<Goodsorder>() {
			public Update generate(Goodsorder t) {
				return update(GOODSORDER_TABLE).set(GOODSORDER_TABLE.USER_ID.value(t.getUserId()),
						GOODSORDER_TABLE.GOODS_ID.value(t.getGoodsId()), GOODSORDER_TABLE.OUT_TIME.value(t.getOutTime())

				).where(GOODSORDER_TABLE.ORDER_ID.eq(t.getOrderId()));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] batchDelete(List<Goodsorder> goodsorders) {
		if (CollectionUtil.isEmpty(goodsorders)) {
			return new int[0];
		}
		for (Goodsorder goodsorder : goodsorders) {
			if (

			StringUtils.isBlank(goodsorder.getOrderId()) &&

					StringUtils.isBlank(goodsorder.getUserId()) &&

					StringUtils.isBlank(goodsorder.getGoodsId()) &&

					StringUtils.isBlank(String.valueOf(goodsorder.getOutTime()))

			) {
				return new int[0];
			}
		}
		return getDslTemplate().batchDelete(goodsorders, new DeleteGenerateCallback<Goodsorder>() {
			public Delete generate(Goodsorder t) {
				return delete(GOODSORDER_TABLE).where(and(GOODSORDER_TABLE.ORDER_ID.eq(t.getOrderId()),
						GOODSORDER_TABLE.USER_ID.eq(t.getUserId()), GOODSORDER_TABLE.GOODS_ID.eq(t.getGoodsId()),
						GOODSORDER_TABLE.OUT_TIME.eq(t.getOutTime())

				));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] preparedBatchInsert(boolean autoGeneratedKeys, List<Goodsorder> goodsorder) {
		if (CollectionUtil.isEmpty(goodsorder)) {
			return new int[0];
		}
		return getDslTemplate().batchInsert(autoGeneratedKeys, goodsorder, new NoParamInsertGenerateCallback() {

			public Insert generate() {
				return insertInto(GOODSORDER_TABLE).values(
						GOODSORDER_TABLE.USER_ID.value(new JdbcNamedParameter("userId")),
						GOODSORDER_TABLE.GOODS_ID.value(new JdbcNamedParameter("goodsId")),
						GOODSORDER_TABLE.OUT_TIME.value(new JdbcNamedParameter("outTime"))

				);
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] preparedBatchUpdate(List<Goodsorder> goodsorders) {
		if (CollectionUtil.isEmpty(goodsorders)) {
			return new int[0];
		}

		for (Goodsorder goodsorder : goodsorders) {
			if (StringUtils.isBlank(goodsorder.getOrderId())) {
				return new int[0];
			}
		}
		return getDslTemplate().batchUpdate(goodsorders, new NoParamUpdateGenerateCallback() {
			public Update generate() {
				return update(GOODSORDER_TABLE).set(GOODSORDER_TABLE.USER_ID.value(new JdbcNamedParameter("userId")),
						GOODSORDER_TABLE.GOODS_ID.value(new JdbcNamedParameter("goodsId")),
						GOODSORDER_TABLE.OUT_TIME.value(new JdbcNamedParameter("outTime"))

				).where(GOODSORDER_TABLE.ORDER_ID.eq(new JdbcNamedParameter("orderId")));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] preparedBatchDelete(List<Goodsorder> goodsorders) {
		if (CollectionUtil.isEmpty(goodsorders)) {
			return new int[0];
		}

		for (Goodsorder goodsorder : goodsorders) {
			if (

			StringUtils.isBlank(goodsorder.getOrderId()) &&

					StringUtils.isBlank(goodsorder.getUserId()) &&

					StringUtils.isBlank(goodsorder.getGoodsId()) &&

					StringUtils.isBlank(String.valueOf(goodsorder.getOutTime()))

			) {
				return new int[0];
			}
		}

		return getDslTemplate().batchDelete(goodsorders, new NoParamDeleteGenerateCallback() {
			public Delete generate() {
				return delete(GOODSORDER_TABLE).where(and(GOODSORDER_TABLE.USER_ID.eq(new JdbcNamedParameter("userId")),
						GOODSORDER_TABLE.GOODS_ID.eq(new JdbcNamedParameter("goodsId")),
						GOODSORDER_TABLE.OUT_TIME.eq(new JdbcNamedParameter("outTime"))

				));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] preparedBatchInsert(List<Goodsorder> goodsorder) {
		return preparedBatchInsert(false, goodsorder);
	}

}
